package org.wzq.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.wzq.mdm.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * project:
 * author: wzq
 * date: 2014/8/9
 * description:
 */
public class ActTestList extends Activity {
    private Context mContext;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_test_list);
        mContext = getApplicationContext();
        final ListView list = (ListView) findViewById(R.id.list_of_samples);

        // 分割线 xml android:divider dividerHeight
        //防止滑动时 背景变黑 android:cacheColorHint="#00000000" 变黑的原因是系统做了性能优化
        list.setCacheColorHint(Color.TRANSPARENT);
        SimpleAdapter adapter = new SimpleAdapter(mContext,
                getData(),
                R.layout.list_struct,
                new String[]{"title"},
                new int[]{R.id.text});
        list.setAdapter(adapter);
        // 点击事件
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Map map = (Map) list.getItemAtPosition(position);
                Intent intent = (Intent) map.get("intent");
                startActivity(intent);
            }
        });

    }

    // 获取 action_main 且 category = 预设值的activity intent
    protected List getData() {
        List<Map> sampleList = new ArrayList<Map>();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory("org.wzq.test");
        //mainIntent.addCategory(getResources().getString(R.string.test_activity));
        PackageManager pm = getPackageManager();
        List<ResolveInfo> info = pm.queryIntentActivities(mainIntent, 0);
        if (info == null) {
            return sampleList;
        }

        //
        Map<String, Object> map;
        int len = info.size();
        for (int i = 0; i < len; i++) {
            //get label
            ResolveInfo label = info.get(i);
            CharSequence titleSeq = label.loadLabel(pm);
            String title = (titleSeq != null) ? titleSeq.toString() : label.activityInfo.name;
            map = new HashMap<String, Object>();

            //put title
            map.put("title", title);

            //put intent
            Intent intent = new Intent();
            String packageName = label.activityInfo.applicationInfo.packageName;
            String className = label.activityInfo.name;
            intent.setClassName(packageName, className);
            map.put("intent", intent);

            //add to list
            sampleList.add(map);
        }
        return sampleList;
    }
}
